<?php

namespace App\Services\Order;

use App\Events\MedalEvent;
use App\Events\TaskEvent;
use App\Models\Activity\Code;
use App\Models\Activity\Join;
use App\Models\Activity\Ticket;
use App\Models\Common\HandleLog;
use App\Models\Member\Member;
use App\Models\Order\Money;
use App\Models\Order\Order;
use App\Services\Member\FreezeMoneyService;
use App\Services\Member\MoneyService;
use Illuminate\Support\Facades\DB;

class OrderService
{
    //钱包明细数据来源类型
    // 1充值  //固定图标
    // 2提现  //固定图标
    // 8余额提现退回(审核不通过) //固定图标
    // 200其他 // 固定图标
    // 3广告收入  //固定图标
    //
    // 7活动支出   5打赏支出  自己的用户头像
    // 4打赏收入 //来源用户头像
    // 6活动收入- （活动名称） // 活动头图
    /**
     * 订单支付成功回调
     * @param Order $order
     * @return bool
     * @throws \Exception
     */
    public static function updateStateSuccess(Order $order)
    {
        try {
            if($order->state == 1) return true;

            DB::beginTransaction();

            switch ($order->type) {
                case 1:  //充值订单
                    //充值用户账户
                    MoneyService::income([$order->id], $order->member_uuid, $order->cash, 1, '充值', 'https://migo-pro.oss-cn-shanghai.aliyuncs.com/admin/image/2023-01-05/%E5%85%85%E5%80%BC%403x.png');
                    //更新子订单状态
                    Money::where('order_uuid', $order->uuid)->update(['state' => 1]);
                    break;
                case 2:  //活动报名订单
                    $member = Member::where('uuid', $order->member_uuid)->first();
                    switch ($order->pay_way) {
                        case 'money':
                            MoneyService::out($order->member_uuid, $order->cash, 7, '活动报名', $member->avatar_url);
                    }

                    //增加活动举办方（用户）冻结金
                    $joinOrder = Join::where('order_uuid', $order->uuid)->with('activity')->first();
                    $freezeMoney = bcmul($order->rece, 1, 2);

                    if($freezeMoney > 0){
                        FreezeMoneyService::income([$order->id], $joinOrder->activity->member_uuid, $freezeMoney, 3, '活动报名收入-'.$joinOrder->activity->name, $member->avatar_url ?? '', $joinOrder->activity_uuid);
                    }

                    $join = Join::where('order_uuid', $order->uuid)->with('activity')->first();
                    $join->state = 1;
//                    $join->update(['state' => 1]);


                    //修改报名人数统计
                    //免费活动
                    if($join->activity->is_free == 1){
                        //更新活动报名人数
                        $join->activity->increment('residue_stock');
                    }else{
                        //更新活动报名人数
                        Ticket::where('uuid', $join->ticket_uuid)->increment('residue_stock');
                    }

                    $code = Code::where('code', $join->code)->where('activity_uuid', $join->activity_uuid)->first();
                    if(isset($code) && !empty($code)) {
                        $code->decrement('use_num');
                        $code->increment('used_num');
                        $join->code_num = $code->used_num;
                    };

                    //更新子订单状态
                    $join->save();

                    event(new TaskEvent(15, $order->member_uuid));

                    event(new MedalEvent(20, $order->member_uuid));

                    break;
            }

            //更新订单状态
            $order->update(['state' => 1]);

            //生成流转日志
            HandleLog::create([
                'service_uuid' => $order->uuid,
                'dataJson' => json_encode($order),
                'msg' => '支付成功',
            ]);

            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();

            throw new \Exception($e->getMessage());
        }

        return true;
    }

}
